package com.ipan.weixin.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType;

import com.ipan.weixin.util.WxHttpUtils;
import com.ipan.weixin.util.WxStringUtils;
import javax.swing.UIManager;

/**
 * 调试界面
 * 
 * @author iPan
 * @version 2013-12-30
 */
public class DebugFrame extends JFrame {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 6446677240323870846L;
	private static final String BODY_SEPARATOR = WxStringUtils.EOL + "// -- end -- //" + WxStringUtils.EOL; 
	private JPanel contentPane;
	private JTextField textUrl;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		JFrame.setDefaultLookAndFeelDecorated(true);
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					DebugFrame frame = new DebugFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public DebugFrame() {
		setTitle("Http Debug");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 800, 700);
		initLocation(); // 居中显示
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));
		
		JPanel panelNorth = new JPanel();
		panelNorth.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "\u8BF7\u6C42", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		contentPane.add(panelNorth, BorderLayout.NORTH);
		panelNorth.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_1 = new JPanel();
		panelNorth.add(panel_1, BorderLayout.NORTH);
		panel_1.setLayout(new GridLayout(2, 1, 0, 0));
		
		JPanel panel_1_1 = new JPanel();
		panel_1.add(panel_1_1);
		panel_1_1.setLayout(new BorderLayout(0, 0));
		
		JLabel lblUrl = new JLabel("URL：");
		panel_1_1.add(lblUrl, BorderLayout.WEST);
		
		textUrl = new JTextField();
		panel_1_1.add(textUrl);
		textUrl.setColumns(80);
		
		JPanel panel_1_2 = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel_1_2.getLayout();
		flowLayout.setAlignment(FlowLayout.LEFT);
		panel_1.add(panel_1_2);
		
		JLabel labEncode = new JLabel("编码：");
		panel_1_2.add(labEncode);
		
		final JComboBox cbxEncode = new JComboBox();
		panel_1_2.add(cbxEncode);
		cbxEncode.setModel(new DefaultComboBoxModel(new String[] {"UTF-8", "GBK", "ISO-88591"}));
		
		JLabel labMime = new JLabel("MIME：");
		panel_1_2.add(labMime);
		
		final JComboBox cbxMime = new JComboBox();
		cbxMime.setModel(new DefaultComboBoxModel(new String[] {"text/xml", "text/html", "text/plain", "application/json"}));
		panel_1_2.add(cbxMime);
		
		JPanel panel_2 = new JPanel();
		panelNorth.add(panel_2);
		panel_2.setLayout(new BorderLayout(0, 0));
		
		JLabel label_1 = new JLabel("正文：");
		panel_2.add(label_1, BorderLayout.WEST);
		
		JPanel panel_2_1 = new JPanel();
		panel_2.add(panel_2_1, BorderLayout.CENTER);
		panel_2_1.setLayout(new BorderLayout(0, 0));
		
		final JTextArea textIn = new JTextArea();
		textIn.setLineWrap(true);
		textIn.setColumns(80);
		textIn.setRows(10);
		
		JScrollPane scrollPane = new JScrollPane(textIn);
		panel_2_1.add(scrollPane, BorderLayout.CENTER);
		
		JPanel panelCenter = new JPanel();
		panelCenter.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "\u54CD\u5E94", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panelCenter.setToolTipText("");
		contentPane.add(panelCenter, BorderLayout.CENTER);
		panelCenter.setLayout(new BorderLayout(0, 0));
		
		final JTextArea textOut = new JTextArea();
		textOut.setColumns(80);
		textOut.setRows(10);
		textOut.setLineWrap(true);
		
		JScrollPane scrollPane_out = new JScrollPane(textOut);
		panelCenter.add(scrollPane_out, BorderLayout.CENTER);
		
		JPanel panelSouth = new JPanel();
		panelSouth.setBorder(new TitledBorder(null, "\u63A7\u5236", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		contentPane.add(panelSouth, BorderLayout.SOUTH);
		
		JButton btnPost = new JButton("post");
		btnPost.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String uri = textUrl.getText();
				String entityStr = textIn.getText();
				String encodeStr = cbxEncode.getSelectedItem().toString();
				String mimeStr = cbxMime.getSelectedItem().toString();
				String result = null;
				if (StringUtils.isEmpty(entityStr)) { 
					result = WxHttpUtils.post(uri);
				} else { // 需要提交文本实体
					ContentType contentType = WxHttpUtils.createContentType(mimeStr, encodeStr);
					HttpEntity entity = WxHttpUtils.createStringEntity(entityStr, contentType);
					result = WxHttpUtils.post(uri, null, entity);
				}
				// 设置输出内容
				if (result != null) {
					if (StringUtils.isEmpty(textOut.getText())) {
						textOut.setText(result);
					} else {
						textOut.setText(textOut.getText() + BODY_SEPARATOR + result);
					}
				}
			}
		});
		panelSouth.add(btnPost);
		
		JButton btnGet = new JButton("get");
		btnGet.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String uri = textUrl.getText();
				String result = WxHttpUtils.get(uri);
				// 设置输出内容
				if (result != null) {
					if (StringUtils.isEmpty(textOut.getText())) {
						textOut.setText(result);
					} else {
						textOut.setText(textOut.getText() + BODY_SEPARATOR + result);
					}
				}
			}
		});
		panelSouth.add(btnGet);
		
		JButton btnClear = new JButton("clear");
		btnClear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				textOut.setText("");
			}
		});
		panelSouth.add(btnClear);
		
		JButton btnClose = new JButton("close");
		btnClose.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		panelSouth.add(btnClose);
	}

	private void initLocation() {
		Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();
		int x = (int) (scrSize.getWidth() - super.getWidth()) / 2;
		int y = (int) (scrSize.getHeight() - super.getHeight()) / 2;
		this.setLocation(x, y);
	}
}
